package com.epower.web.action.epowerwordforday;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import com.epower.base.BaseAction;
import com.epower.bo.epower.commons.Result;
import com.epower.bo.epowersentence.EpowerSentenceService;
import com.epower.bo.epowerwordforday.EpowerWordForDayService;
import com.epower.dataobject.EpowerWordForDayDO;

@Scope("prototype")
@Component()
public class EpowerWordForDayAction extends BaseAction {

    private static final long serialVersionUID = 1L;

    /*** 查询对象 ***/
    private EpowerWordForDayDO query;

    /*** 对象集合 ***/
    private List<EpowerWordForDayDO> epowerWordForDayList;

    /*** BO ***/
    private EpowerWordForDayService epowerWordForDayBO;
    private EpowerSentenceService epowerSentenceBO;

    /*** 主键 ***/
    private Integer id;
    /*** 查询对象 ***/
    private EpowerWordForDayDO epowerWordForDay;

    private List<String> sentence = new ArrayList<String>();
    /*** 当前action所在的namespace ***/
    private String namespace = "epowerwordforday";
    /*** 当前action对应的module ***/
    private String moduleName = "EpowerWordForDay";

    private Integer haveSentence;
    private String spelling;
    private Result result = Result.succResult();

    /***
     * 列表视图
     * 
     * @return
     * @throws Exception
     */
    public String listEpowerWordForDay() throws Exception {
        // 初始化查询条件
        if (query == null) {
            query = new EpowerWordForDayDO();
            query.setStatus(1);
        }
        // 查询列表
        epowerWordForDayList = epowerWordForDayBO.getEpowerWordForDayListPageByOption(query);
        pages = this.getPagesList(query.getToPage(), query.getTotalPage());
        return SUCCESS;
    }

    /***
     * 编辑视图
     * 
     * @return
     * @throws Exception
     */
    public String viewEditEpowerWordForDay() throws Exception {
        epowerWordForDay = epowerWordForDayBO.getEpowerWordForDayById(id);
        haveSentence = epowerSentenceBO.deleteEpowerSentenceBySpelling(spelling);
        return SUCCESS;
    }

    /***
     * 编辑操作
     * 
     * @return
     * @throws Exception
     */

    public String editEpowerWordForDay() throws Exception {
        if (!epowerWordForDayBO.updateEpowerWordForDayByOption(epowerWordForDay, sentence)) {
            addActionMessage("编辑失败");
            return SUCCESS;
        }
        addActionMessage("编辑成功");
        return SUCCESS;
    }

    /***
     * 删除操作
     * 
     * @return
     * @throws Exception
     */
    public String deleteEpowerWordForDay() throws Exception {
        if (!epowerWordForDayBO.deleteEpowerWordForDayById(id)) {
            addActionMessage("删除失败");
            return SUCCESS;
        }
        addActionMessage("删除成功");
        return SUCCESS;
    }

    /***
     * 添加视图
     * 
     * @return
     * @throws Exception
     */
    public String viewAddEpowerWordForDay() throws Exception {
        return SUCCESS;
    }

    public String viewqueryMainEpowerWord()throws Exception{
        return SUCCESS;
    }
    /***
     * 添加操作
     * 
     * @return
     * @throws Exception
     */
    public String addEpowerWordForDay() throws Exception {
        
        if (epowerWordForDayBO.addEpowerWordForDay(epowerWordForDay, sentence) == null) {
            addActionMessage("添加失败");
            return SUCCESS;
        }
        addActionMessage("添加成功");
        return SUCCESS;
    }

    public String queryMainEpowerWordBySpelling() throws Exception {
        // 初始化查询条件
        if (spelling == null) {
            return ERROR;
        }
        List<EpowerWordForDayDO> mainWords = epowerWordForDayBO.getMainEpowerWordForDayBySpelling(spelling);
        List<TmpWord> tmps = new ArrayList<TmpWord>();
        for (EpowerWordForDayDO tmp : mainWords) {
            TmpWord tmpword = new TmpWord();
            tmpword.setId(tmp.getId());
            tmpword.setSpelling(tmp.getSpelling());
            tmps.add(tmpword);
        }
        Map<String, Object> resultMap = new HashMap<String, Object>();
        resultMap.put("mainwords", tmps);
        result.setResult(resultMap);
        return SUCCESS;
    }

    /*** setter getter start ***/
    public EpowerWordForDayDO getQuery() {
        return query;
    }

    public void setQuery(EpowerWordForDayDO query) {
        this.query = query;
    }

    public List<EpowerWordForDayDO> getEpowerWordForDayList() {
        return epowerWordForDayList;
    }

    public void setEpowerWordForDayList(List<EpowerWordForDayDO> epowerWordForDayList) {
        this.epowerWordForDayList = epowerWordForDayList;
    }

    public EpowerWordForDayService getEpowerWordForDayBO() {
        return epowerWordForDayBO;
    }

    @Resource
    public void setEpowerWordForDayBO(EpowerWordForDayService epowerWordForDayBO) {
        this.epowerWordForDayBO = epowerWordForDayBO;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public EpowerWordForDayDO getEpowerWordForDay() {
        return epowerWordForDay;
    }

    public void setEpowerWordForDay(EpowerWordForDayDO epowerWordForDay) {
        this.epowerWordForDay = epowerWordForDay;
    }

    public String getNamespace() {
        return namespace;
    }

    public void setNamespace(String namespace) {
        this.namespace = namespace;
    }

    public String getModuleName() {
        return moduleName;
    }

    public void setModuleName(String moduleName) {
        this.moduleName = moduleName;
    }

    public List<String> getSentence() {
        return sentence;
    }

    public void setSentence(List<String> sentence) {
        this.sentence = sentence;
    }

    public Integer getHaveSentence() {
        return haveSentence;
    }

    public void setHaveSentence(Integer haveSentence) {
        this.haveSentence = haveSentence;
    }

    public EpowerSentenceService getEpowerSentenceBO() {
        return epowerSentenceBO;
    }

    public void setEpowerSentenceBO(EpowerSentenceService epowerSentenceBO) {
        this.epowerSentenceBO = epowerSentenceBO;
    }

    public String getSpelling() {
        return spelling;
    }

    public void setSpelling(String spelling) {
        this.spelling = spelling;
    }

    public Result getResult() {
        return result;
    }

    public void setResult(Result result) {
        this.result = result;
    }
    
    /*** setter getter end ***/
    public class TmpWord {

        private Integer id;
        private String spelling;

        public Integer getId() {
            return id;
        }

        public void setId(Integer id) {
            this.id = id;
        }

        public String getSpelling() {
            return spelling;
        }

        public void setSpelling(String spelling) {
            this.spelling = spelling;
        }

    }
}
